package com.cybertron.ironhide.search.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cybertron.ironhide.search.domain.PriceAlert;
import com.cybertron.ironhide.search.persistence.PriceAlertMapper;
import com.cybertron.ironhide.search.service.PriceAlertService;
import com.cybertron.ironhide.search.util.CommonUtil;

@Service
public class PriceAlertServiceImpl implements PriceAlertService {

	@Autowired(required = false)
	private PriceAlertMapper priceAlertMapper;

	@Override
	public void insertSelective(PriceAlert record) {
		PriceAlert persitAlert = priceAlertMapper.selectByEmailAndProductId(
				record.getEmail(), record.getProductId());
		if (persitAlert != null) {
			persitAlert.setPriceAlert(record.getPriceAlert());
			persitAlert.setModifyDate(new Date());
			priceAlertMapper.updateByPrimaryKeySelective(persitAlert);
		} else {
			record.setId(CommonUtil.getUUID());
			record.setCreateDate(new Date());
			record.setModifyDate(new Date());
			priceAlertMapper.insertSelective(record);
		}
	}
}
